layui.use(['form','tree','element','table','laytpl','treeGrid','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    //回显数据
    var request = GetRequest();

    //列表数据
    var option = {
        elem: '#tableList',
        url : '/equipment/channel/query?equipmentId=' + request.id,
        cellMinWidth : 95,
        page : true,
        height : "full",
        limits : [10,15,20,25],
        limit : 25,
        id : "listTable",
        /*initSort: {
            field: 'channelNo' //排序字段，对应 cols 设定的各字段名
            ,type: 'asc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
        },*/
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'channelId', title: '通道编号', minWidth:100, align:"center"},
            {field: 'channelNo', title: '通道序号', minWidth:100, align:"center"},
            {field: 'channelName', title: '通道名称', minWidth:200, align:'center'},
            {field: 'originalDataUnit', title: '数据单位',  align:'center'},
            {field: 'remarks', title: '备注', align:'center'},
            {field: 'createDate', title: '创建时间', align:'center',minWidth:150,sort:true, templet:function(d){
                    //处理时间
                    var date = new Date(d.createDate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {title: '操作', minWidth:175, templet:'#tableListBar',fixed:"right",align:"center"}
        ]]
    };

    //设备列表
    var tableIns = table.render(option);

    //搜索设备信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/equipment/query';
            option.method = 'post';
            option.where = {name: searchKey};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加设备通道
    $(".addBtn").click(function(){
        var index = layui.layer.open({
            title : "添加设备通道",
            type : 2,
            content : "colChannelAdd?equipmentId=" + request.id,
            area: ['800px',$(document).height() - 100 + 'px'],
            success : function(layero, index){

            }
        });
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].relChannelId);
            }
            layer.confirm('确定删除选中的设备？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/equipment/channel/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的设备");
        }
    });

    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "修改设备通道信息",
                type : 2,
                content : "colChannelEdit?id=" + data.relChannelId,
                area: ['800px',$(document).height() - 100 + 'px'],
                success : function(layero, index){
                }
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此设备？',{icon:3, title:'提示信息'},function(index){
                $.get("/equipment/channel/delete?id=" + data.relChannelId,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});